<?php

namespace App\Models;

use Dcat\Admin\Admin;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;

class Customer extends Base
{
	use HasDateTimeFormatter;
    use SoftDeletes;

    const GENDER = [
        0   =>  '未知',
        1   =>  '男',
        2   =>  '女',
    ];

    const STATUS = [
        100 => '待跟进',
        101 => '待上架',
        102 => '已上架',
        103 => '已出售',
        104 => '已出租',
        105 => '自住',
    ];

    const LEVEL = [
        100 => '潜在',
        101 => 'A类',
        102 => 'B类',
        103 => 'C类',
        104 => 'D类',
    ];

    const TYPE = [

    ];


    public function property()
    {
        return $this->hasMany(Property::class,'customer_id','id');
    }

    public function followup()
    {
        return $this->hasMany(Followup::class,'customer_id','id');
    }

    public function user()
    {
        return $this->belongsTo(User::class,'user_id','id');
    }

    //转入公海
    public static function ooaInto($ids)
    {
        $data['user_id']        = 0;
        $data['ooa_time']       = date('Y-m-d H:i:s');
        //生成变更记录
        Customer::query()->where('user_id',Admin::user()->id)->whereIn('id',changeArr($ids))
            ->chunk(200, function ($items) { // 每次处理200条
                $dataToInsert = [];
                foreach ($items as $item) {
                    $dataToInsert[] = [
                        'customer_id'   => $item->id,
                        'from_user_id'  => $item->user_id,
                        'to_user_id'    => 0,
                        'type'          => 5,
                        'reason'        => CustomerAssignment::TYPE[4],
                        'created_at'    => date('Y-m-d H:i:s'),
                    ];
                }
                CustomerAssignment::query()->insert($dataToInsert);
            });
        return self::query()
            ->where('user_id',Admin::user()->id)
            ->whereIn('id',changeArr($ids))
            ->update($data);
    }

    //公海领取
    public static function ooaReceive($ids)
    {
        $data['user_id']    = Admin::user()->id;
        $data['claimed_at'] = date('Y-m-d H:i:s');

        //生成变更记录
        Customer::query()->where('user_id',0)->whereIn('id',changeArr($ids))
            ->chunk(200, function ($items) { // 每次处理200条
                $dataToInsert = [];
                foreach ($items as $item) {
                    $dataToInsert[] = [
                        'customer_id'   => $item->id,
                        'from_user_id'  => $item->user_id,
                        'to_user_id'    => Admin::user()->id,
                        'type'          => 3,
                        'reason'        => CustomerAssignment::TYPE[1],
                        'created_at'    => date('Y-m-d H:i:s'),
                    ];
                }
                CustomerAssignment::query()->insert($dataToInsert);
            });
        //更改归属人
        return self::query()
            ->where('user_id',0)
            ->whereIn('id',changeArr($ids))
            ->update($data);
    }

}
